# -*- coding: utf-8 -*-

#
# fusion.http.Content
#
# Copyright 2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Harri Hämäläinen <hhamalai@iki.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import cgi, simplejson
from fusion.db.exceptions import Unauthorized
from fusion.http.FusionApp import FusionApp, Response

class Content(FusionApp):
    def do_GET(self):
        def lookup(key):
            return self.db.get_text(self.iid, '%s%s' % (self.path, key))

        

        patterns = {
            '/': self.redirect_to,
            '/content': self.get,
            '/presentation': self.presentation,
        }
        
        return self.match_response(patterns)
    

    def do_PUT(self):
        return self.post_or_put()


    def do_POST(self):
        return self.post_or_put()


    def do_DELETE(self):
        return self.delete()
    
    
    def redirect_to(self):
        response = Response(301)
        response.add_header('Location', '/content/web/index.html')
        return response

    
    def presentation(self):
        viewer = self.db.get(self.iid, 'info/viewer.txt')
        if not viewer:
            return Response(404)
        
        response = Response()
        safe = u'/' + u'/'.join([u'public'] + [x for x in self.path.split(u'/') if len(x) > 0])
        response.append_output(self.db.get(viewer, safe))
        return response
            
